home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gawk / gawk213s.zoo / gawk-src-2.13 / missing / random.c < prev    next >
C/C++ Source or Header  |  1991-07-21  |  13KB  |  380 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #if defined(LIBC_SCCS) && !defined(lint)
  19. static char sccsid[] = "@(#)random.c    5.5 (Berkeley) 7/6/88";
  20. #endif /* LIBC_SCCS and not lint */
  21.  
  22. #if 0
  23. #include <stdio.h>
  24. #endif
  25.  
  26. /*
  27.  * random.c:
  28.  * An improved random number generation package.  In addition to the standard
  29.  * rand()/srand() like interface, this package also has a special state info
  30.  * interface.  The initstate() routine is called with a seed, an array of
  31.  * bytes, and a count of how many bytes are being passed in; this array is then
  32.  * initialized to contain information for random number generation with that
  33.  * much state information.  Good sizes for the amount of state information are
  34.  * 32, 64, 128, and 256 bytes.  The state can be switched by calling the
  35.  * setstate() routine with the same array as was initiallized with initstate().
  36.  * By default, the package runs with 128 bytes of state information and
  37.  * generates far better random numbers than a linear congruential generator.
  38.  * If the amount of state information is less than 32 bytes, a simple linear
  39.  * congruential R.N.G. is used.
  40.  * Internally, the state information is treated as an array of longs; the
  41.  * zeroeth element of the array is the type of R.N.G. being used (small
  42.  * integer); the remainder of the array is the state information for the
  43.  * R.N.G.  Thus, 32 bytes of state information will give 7 longs worth of
  44.  * state information, which will allow a degree seven polynomial.  (Note: the
  45.  * zeroeth word of state information also has some other information stored
  46.  * in it -- see setstate() for details).
  47.  * The random number generation technique is a linear feedback shift register
  48.  * approach, employing trinomials (since there are fewer terms to sum up that
  49.  * way).  In this approach, the least significant bit of all the numbers in
  50.  * the state table will act as a linear feedback shift register, and will have
  51.  * period 2^deg - 1 (where deg is the degree of the polynomial being used,
  52.  * assuming that the polynomial is irreducible and primitive).  The higher
  53.  * order bits will have longer periods, since their values are also influenced
  54.  * by pseudo-random carries out of the lower bits.  The total period of the
  55.  * generator is approximately deg*(2**deg - 1); thus doubling the amount of
  56.  * state information has a vast influence on the period of the generator.
  57.  * Note: the deg*(2**deg - 1) is an approximation only good for large deg,
  58.  * when the period of the shift register is the dominant factor.  With deg
  59.  * equal to seven, the period is actually much longer than the 7*(2**7 - 1)
  60.  * predicted by this formula.
  61.  */
  62.  
  63.  
  64.  
  65. /*
  66.  * For each of the currently supported random number generators, we have a
  67.  * break value on the amount of state information (you need at least this
  68.  * many bytes of state info to support this random number generator), a degree
  69.  * for the polynomial (actually a trinomial) that the R.N.G. is based on, and
  70.  * the separation between the two lower order coefficients of the trinomial.
  71.  */
  72.  
  73. #define        TYPE_0        0        /* linear congruential */
  74. #define        BREAK_0        8
  75. #define        DEG_0        0
  76. #define        SEP_0        0
  77.  
  78. #define        TYPE_1        1        /* x**7 + x**3 + 1 */
  79. #define        BREAK_1        32
  80. #define        DEG_1        7
  81. #define        SEP_1        3
  82.  
  83. #define        TYPE_2        2        /* x**15 + x + 1 */
  84. #define        BREAK_2        64
  85. #define        DEG_2        15
  86. #define        SEP_2        1
  87.  
  88. #define        TYPE_3        3        /* x**31 + x**3 + 1 */
  89. #define        BREAK_3        128
  90. #define        DEG_3        31
  91. #define        SEP_3        3
  92. #ifdef _CRAY
  93. #define        DEG_3_P1    32        /* bug - do addition here */
  94. #define        SEP_3_P1    4        /* *_3 + 1 = _3_P1 */
  95. #endif
  96.  
  97. #define        TYPE_4        4        /* x**63 + x + 1 */
  98. #define        BREAK_4        256
  99. #define        DEG_4        63
  100. #define        SEP_4        1
  101.  
  102.  
  103. /*
  104.  * Array versions of the above information to make code run faster -- relies
  105.  * on fact that TYPE_i == i.
  106.  */
  107.  
  108. #define        MAX_TYPES    5        /* max number of types above */
  109.  
  110. static  int        degrees[ MAX_TYPES ]    = { DEG_0, DEG_1, DEG_2,
  111.                                 DEG_3, DEG_4 };
  112.  
  113. static  int        seps[ MAX_TYPES ]    = { SEP_0, SEP_1, SEP_2,
  114.                                 SEP_3, SEP_4 };
  115.  
  116.  
  117.  
  118. /*
  119.  * Initially, everything is set up as if from :
  120.  *        initstate( 1, &randtbl, 128 );
  121.  * Note that this initialization takes advantage of the fact that srandom()
  122.  * advances the front and rear pointers 10*rand_deg times, and hence the
  123.  * rear pointer which starts at 0 will also end up at zero; thus the zeroeth
  124.  * element of the state information, which contains info about the current
  125.  * position of the rear pointer is just
  126.  *    MAX_TYPES*(rptr - state) + TYPE_3 == TYPE_3.
  127.  */
  128.  
  129. static  long        randtbl[ DEG_3 + 1 ]    = { TYPE_3,
  130.                 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342,
  131.                 0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb,
  132.                 0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd,
  133.                 0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86,
  134.                 0xda672e2a, 0x1588ca88, 0xe369735d, 0x904f35f7,
  135.                 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc,
  136.                 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b,
  137.                     0xf5ad9d0e, 0x8999220b, 0x27fb47b9 };
  138.  
  139. /*
  140.  * fptr and rptr are two pointers into the state info, a front and a rear
  141.  * pointer.  These two pointers are always rand_sep places aparts, as they cycle
  142.  * cyclically through the state information.  (Yes, this does mean we could get
  143.  * away with just one pointer, but the code for random() is more efficient this
  144.  * way).  The pointers are left positioned as they would be from the call
  145.  *            initstate( 1, randtbl, 128 )
  146.  * (The position of the rear pointer, rptr, is really 0 (as explained above
  147.  * in the initialization of randtbl) because the state table pointer is set
  148.  * to point to randtbl[1] (as explained below).
  149.  */
  150.  
  151. #ifdef _CRAY
  152. static  long        *fptr            = &randtbl[ SEP_3_P1 ];
  153. #else
  154. static  long        *fptr            = &randtbl[ SEP_3 + 1 ];
  155. #endif
  156. static  long        *rptr            = &randtbl[ 1 ];
  157.  
  158.  
  159.  
  160. /*
  161.  * The following things are the pointer to the state information table,
  162.  * the type of the current generator, the degree of the current polynomial
  163.  * being used, and the separation between the two pointers.
  164.  * Note that for efficiency of random(), we remember the first location of
  165.  * the state information, not the zeroeth.  Hence it is valid to access
  166.  * state[-1], which is used to store the type of the R.N.G.
  167.  * Also, we remember the last location, since this is more efficient than
  168.  * indexing every time to find the address of the last element to see if
  169.  * the front and rear pointers have wrapped.
  170.  */
  171.  
  172. static  long        *state            = &randtbl[ 1 ];
  173.  
  174. static  int        rand_type        = TYPE_3;
  175. static  int        rand_deg        = DEG_3;
  176. static  int        rand_sep        = SEP_3;
  177.  
  178. #ifdef _CRAY
  179. static  long        *end_ptr        = &randtbl[ DEG_3_P1 ];
  180. #else
  181. static  long        *end_ptr        = &randtbl[ DEG_3 + 1 ];
  182. #endif
  183.  
  184.  
  185.  
  186. /*
  187.  * srandom:
  188.  * Initialize the random number generator based on the given seed.  If the
  189.  * type is the trivial no-state-information type, just remember the seed.
  190.  * Otherwise, initializes state[] based on the given "seed" via a linear
  191.  * congruential generator.  Then, the pointers are set to known locations
  192.  * that are exactly rand_sep places apart.  Lastly, it cycles the state
  193.  * information a given number of times to get rid of any initial dependencies
  194.  * introduced by the L.C.R.N.G.
  195.  * Note that the initialization of randtbl[] for default usage relies on
  196.  * values produced by this routine.
  197.  */
  198.  
  199. srandom( x )
  200.  
  201.     unsigned        x;
  202. {
  203.         register  int        i, j;
  204.     long random();
  205.  
  206.     if(  rand_type  ==  TYPE_0  )  {
  207.         state[ 0 ] = x;
  208.     }
  209.     else  {
  210.         j = 1;
  211.         state[ 0 ] = x;
  212.         for( i = 1; i < rand_deg;